package top.jlzhou.jobs.service.impl;

import top.jlzhou.jobs.entity.JobInfo;
import top.jlzhou.jobs.entity.UserInfo;
import top.jlzhou.jobs.mapper.JobInfoMapper;
import top.jlzhou.jobs.service.JobInfoService;
import top.jlzhou.jobs.service.UserInfoService;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author J.L.Zhou
 * @since 2018-10-22
 */
@Service
public class JobInfoServiceImpl extends ServiceImpl<JobInfoMapper, JobInfo> implements JobInfoService {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserInfoService service;
	@Override
	public JobInfo getNewJob(Long token) throws Exception {
		UserInfo userInfo = service.getByToken(token);
		return baseMapper.selectOne(new QueryWrapper<JobInfo>().eq("user_name", userInfo.getUserName()));
	}
	@Override
	public void saveJob(Long token, JobInfo job) {
		UserInfo userInfo = service.getByToken(token);
		if(job.getJobId()!=null) {
			logger.debug(JSON.toJSONString(job));
			baseMapper.updateById(job);
			
		}else {
			job.setUserName(userInfo.getUserName());
			baseMapper.insert(job);
		}
		
	}

	
}
